<template>
  <div id="Company_contact">
    <span>
      <img src="../assets/enterprise.png" alt="" class="bread-icon">
    </span>
    <el-breadcrumb style="display:inline-block">
      <el-breadcrumb-item>
        <span style="font-size:20px;">企业信息</span>
      </el-breadcrumb-item>
      <el-breadcrumb-item>
        <span style="font-size:15px;line-height:20px;">联系人信息</span>
      </el-breadcrumb-item>
    </el-breadcrumb>
    <div class="content-wrap" v-loading='loading'>
      <div class="topNav">
        <span class="navLeft">联系人信息</span>
        <!-- <span class="navRight">
          <el-button size="small" @click="dragSearch()">搜索</el-button>
        </span>
        <span class="navMid">
          <img class="icon-search" src="../assets/search.png" alt="">
          <el-input v-model="searchData" size="small" placeholder="请输入内容" class="searchInput"></el-input>
        </span> -->
      </div>
      <el-form :inline="true" ref="contact_info" class="form-common" :model="contact_info" label-width="130px">
        <el-form-item label="企业id">
          <el-input v-model="contact_info.enterprise" placeholder="企业id"></el-input>
        </el-form-item>
        <el-form-item label="企业指定联系人">
          <el-input v-model="contact_info.linkman" placeholder="企业指定联系人"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="联系电话">
          <el-input v-model="contact_info.tel" placeholder="联系电话"></el-input>
        </el-form-item>
        <el-form-item label="传真">
          <el-input v-model="contact_info.fax" placeholder="传真"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="电子邮箱">
          <el-input v-model="contact_info.email" placeholder="电子邮箱"></el-input>
        </el-form-item>
        <el-form-item label="手机">
          <el-input v-model="contact_info.mobile" placeholder="手机"></el-input>
        </el-form-item>
        <el-form-item></el-form-item>
      </el-form>
      <div class="btn-group">
        <el-button @click="getbak">取消</el-button>
        <el-button type="primary" @click="update">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        adminId: '',
        adminToken: '',
        enterprise: '',
        loading: true,
        contact_info: {},
        contact_info_bak: {}
      }
    },
    mounted() {
      this.adminId = this.$store.getters.getInfo.id
      this.adminToken = this.$store.getters.getInfo.token
      this.enterprise = this.$store.getters.getInfo.enterprise
      if (this.adminToken) {
        this.$http({
          url: './enterprise/' + this.enterprise + '/',
          headers: {
            'Authorization': 'Token ' + this.adminToken
          },
          method: 'get',
        }).then(
          response => {
            //success
            if (response.status == '200') {
              this.contact_info_bak = JSON.parse(JSON.stringify(response.data.contact_info))
              this.contact_info = response.data.contact_info
            } else if (response.status == '401') {
              alert('error')
            }
            this.loading = false
          }
        ).catch(error => {
          console.log(error.response)
          if (error.response.status == 401) {
            this.$message({
              message: error.response.data.detail,
              type: 'warning'
            })
            this.$router.push({
              path: '/login'
            });
          } else if (error.response.status == 400) {
            this.$message({
              message: 'error400',
              type: 'error'
            })
          } else {
            this.$message({
              message: '未知错误',
              type: 'error'
            })
          }
          console.log(error);
        });
      } else {
        this.$message({
          message: '登陆已过期，请重新登陆',
          type: 'warning',
          showClose: true,
        });
      }
    },
    methods: {
      getbak() {
        this.contact_info = JSON.parse(JSON.stringify(this.contact_info_bak))
      },
      update() {
        if (this.adminToken) {
          this.$http({
            url: '/enterprise-contact-info/' + this.enterprise + '/',
            headers: {
              'Authorization': 'Token ' + this.adminToken
            },
            method: 'put',
            data: this.contact_info
          }).then(
            response => {
              //success
              console.log(response)
              if (response.status == '200') {
                this.$message({
                  showClose: true,
                  message: '更新成功',
                  type: 'success'
                });
              } else {
                this.contact_info = this.contact_info_bak
                this.$message({
                  showClose: true,
                  message: '更新失败',
                  type: 'warning'
                });
              }
              this.loading = false
            }
          ).catch(error => {
          console.log(error.response)
          if (error.response.status == 401) {
            this.$message({
              message: error.response.data.detail,
              type: 'warning'
            })
            this.$router.push({
              path: '/login'
            });
          } else if (error.response.status == 400) {
            this.$message({
              message: '修改失败',
              type: 'error'
            })
          } else {
            this.$message({
              message: '未知错误',
              type: 'error'
            })
          }
          console.log(error);
        });
        } else {
          this.$message({
            message: '登陆已过期，请重新登陆',
            type: 'warning',
            showClose: true,
          });
        }
      },
    }

  }

</script>

<style scoped>
  #Company_base>>>.el-form-item__content {
    width: 220px;
  }

  .btn-group {
    width: 600px;
    margin: 0 auto;
    text-align: right
  }

</style>
